import common from '../vendor/common'
import '../scss/user_revise_phone2.scss'
import { getCode, resetTel } from '../vendor/api'
try{
    const mui = common.mui
    var data = {}
    var tel = null //新号码
    var sms = null //验证码
    mui.init({
        swipeBack:true //启用右滑关闭功能
    });
    mui.plusReady(function(){
        window.addEventListener('revise', function(e){
            tel = null
            sms = null
            mui('#tel')[0].value = null
            mui('#sms')[0].value = null
        })
        // 获取验证码
        mui('#getSms')[0].addEventListener('tap',function login(e){
            tel = mui('#tel')[0].value
            if(tel){
                // 拼接参数
                var data = tel +'/'+'reset_tel'
                getCode(data).then(function(res){
                    // console.log('asfsdgsdgsdf',JSON.stringify(res))
                    if(res.success){
                        var time = 120; //定义验证码失效的时间
                        mui('#getSms')[0].classList.add('mui-hidden')
                        mui('#time')[0].classList.remove('mui-hidden')
                        mui('#time')[0].innerHTML = time + 's'
                        var outTime = setInterval(() => {
                            time --;
                            if(time <= 0){
                                mui('#getSms')[0].classList.remove('mui-hidden')
                                mui('#time')[0].classList.add('mui-hidden')
                                mui('#getSms')[0].innerHTML = '重新获取验证码'
                                clearInterval(outTime);
                                return;
                            }
                            mui('#time')[0].innerHTML = time + 's'
                        },1000);
                    }
                })
            } else {
                mui.alert('请输入手机号')
            }
        })
        // 绑定新手机号码
        mui('.loginOut').on('tap', 'button', function(){
            // 获取输入框的值
            tel = mui('#tel')[0].value
            sms = mui('#sms')[0].value
            data.tel = tel
            data.smsCode = sms
            // 隐藏键盘
            mui('#tel')[0].blur()
            mui('#sms')[0].blur()
            // 验证并修改
            if(!tel || tel.trim() == ''){
                mui.alert('请输入手机号')
            } else if(!sms || sms.trim() == ''){
                mui.alert('请输入验证码')
            } else {
                resetTel(data).then((res) => {
                    if(res.data.status){
                        // 将修改的电话号码传递给我的首页
                        var initPage = plus.webview.getWebviewById('/home/user_index.html')
                        mui.fire(initPage, 'upTel', {tel: tel})
                        // 修改本地储存电话号码
                        plus.storage.setItem('tel', tel)
                        // 关闭当前页面
                        var ws=plus.webview.currentWebview();
                        plus.webview.hide(ws)
                        // 关闭指定页面回到我的页面
                        var setPage = plus.webview.getWebviewById('user_setting.html') 
                        var phonePage = plus.webview.getWebviewById('user_revise_phone1.html')
                        plus.webview.hide(setPage)
                        plus.webview.hide(phonePage)
                        mui.toast('修改成功')
                    } else {
                        mui.toast(res.data.message)
                    }
                })
            }
        })
    })
}catch(e){
    console.log('user_revise_phone2.html出错了')
}